#! /usr/bin/env python

#  Given prefixes to filenames, assumes they're rows of k1, k2, k3, k4,
# and produces a single list of average k1, k2, k3, k4

import sys, os
import numpy

path = "trials/"
outpath = "analysis/"

number_of_each_file = 1000
n = number_of_each_file
lines_per_file = 20
threshhold = 20

"""
prefixes = ["source_0.1_present",
            "source_1.0_present",
            "source_10.0_present",
            "source_100.0_present",
            "source_1000.0_present",
            "source_absent"]
"""

prefixes = ["source_1.0_present", "source_absent"]
TPR3 = [0] * (threshhold + 1)
TPR60 = [0] * (threshhold + 1)
FPR3 = [0] * (threshhold + 1)
FPR60 = [0] * (threshhold + 1)
ROC3 = []
ROC60 = []

# STORES TPR and FPR for Threshholds 0 ~ 20, for k fusion
if sys.argv[1] == 'k':
	for prefix in prefixes:
		for i in range(n):
			f = open(path + prefix + str(i) + ".txt", "r")
			contents = f.readlines()
			for j in range(len(contents)):
				contents[j] = contents[j].split()
				fusion = [0,0,0]
				for num in range(3):
					if num == 0:
						fusion[num] = float(contents[j][num])/1.01
					elif num == 1:
						fusion[num] = float(contents[j][num])/1.1
					elif num == 2:
						fusion[num] = float(contents[j][num])/1.5
				if j == 0:
					for k in range(21):
						if any(map(lambda d: d > k, fusion)):
							if prefix == "source_1.0_present":
								TPR3[k] += 1
							if prefix == "source_absent":
								FPR3[k] += 1
				if j == 19:
					for k in range(21):
						if any(map(lambda d: d > k, fusion)):
							if prefix == "source_1.0_present":
								TPR60[k] += 1
							if prefix == "source_absent":
								FPR60[k] += 1

			f.close()

	for i in range(threshhold + 1):
		temp = (float(FPR3[i])/n, float(TPR3[i])/n)
		ROC3.append(temp)
		temp = (float(FPR60[i])/n, float(TPR60[i])/n)
		ROC60.append(temp)


	output_roc3 = outpath + "roc3.txt"
	output_roc3_contents = ""
	output_roc60 = outpath + "roc60.txt"
	output_roc60_contents = ""
	
	for i in range(len(ROC3)):
		output_roc3_contents += "%s\t%s\n" % ROC3[i]
		output_roc60_contents += "%s\t%s\n" % ROC60[i]
	
	f = open(output_roc3, "w")
	f.write(output_roc3_contents)
	f.close()

	f = open(output_roc60, "w")
	f.write(output_roc60_contents)
	f.close()
	"""
elif sys.argv[1] == 's':
	for prefix in prefixes:
		output_mean = prefix + "_mean"
		output_var  = prefix + "_var"
		output_mean_contents = ""
		output_var_contents = ""
		arr = numpy.empty((number_of_each_file, lines_per_file, 3))
		for i in range(n):
			f = open(path + prefix + str(i), "r")
			contents = f.readlines()
			for j in range(len(contents)):
				contents[j] = contents[j].split()
			arr[i] = numpy.array(contents)
		means = arr.mean(0)
		vars  = arr.var(0)
		for k in range(len(means)):
			output_mean_contents += "%s\t%s\t%s\n" % tuple(means[k])
			output_var_contents  += "%s\t%s\t%s\n" % tuple(vars[k])
		f = open(output_mean, "w")
		f.write(output_mean_contents)
		f.close()
		f = open(output_var, "w")
		f.write(output_var_contents)
		f.close()
	"""
else:
	print "no can do, boss"
